package com.linxianqin.springbootVue.service.webService;

import com.linxianqin.springbootVue.domain.dto.SysUserDTO;
import com.linxianqin.springbootVue.service.SysMenuService;
import com.linxianqin.springbootVue.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * 功能描述：
 *
 * @author lxq
 * @version 1.00
 * @Date 2020/12/13
 */
@Component
public class SysPermissionService {
    private SysRoleService roleService;
    private SysMenuService menuService;
    public SysPermissionService(SysRoleService roleService,SysMenuService menuService)
    {
        this.roleService=roleService;
        this.menuService=menuService;
    }
    /**
     * 获取菜单数据权限
     *
     * @param userDTO 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUserDTO userDTO)
    {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (userDTO.isAdmin())
        {
            perms.add("*:*:*");
        }
        else
        {
            perms.addAll(menuService.selectMenuPermsByUserId(userDTO.getUserId()));
        }
        return perms;
    }
    /**
     * 获取角色数据权限
     *
     * @param userDTO 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUserDTO userDTO)
    {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (userDTO.isAdmin())
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(userDTO.getUserId()));
        }
        return roles;
    }
}
